package com.example.easytts.controller

import com.example.easytts.model.Voice
import com.example.easytts.repository.VoiceRepository
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/admin/voice")
class VoiceController(private val repo: VoiceRepository) {
    @GetMapping
    fun list(@RequestParam(required = false) languageId: String?) =
        languageId?.let { repo.findByLanguageId(it) } ?: repo.findAll()
    
    @PostMapping 
    fun add(@RequestBody v: Voice) = repo.save(v)
    
    @DeleteMapping("/{id}") 
    fun del(@PathVariable id: Long) = repo.deleteById(id)
    
    @PutMapping("/{id}")
    fun update(@PathVariable id: Long, @RequestBody voice: Voice): ResponseEntity<Any> {
        return try {
            val existingVoice = repo.findById(id).orElse(null)
            if (existingVoice == null) {
                return ResponseEntity.status(404)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(mapOf(
                        "code" to 404,
                        "message" to "声音不存在"
                    ))
            }
            
            val updatedVoice = existingVoice.copy(
                languageId = voice.languageId,
                shortName = voice.shortName,
                localName = voice.localName,
                avatar = voice.avatar,
                gender = voice.gender,
                description = voice.description,
                styleCount = voice.styleCount,
                languageName = voice.languageName,
                audioUrl = voice.audioUrl,
                ageGroups = voice.ageGroups,
                locale = voice.locale
            )
            val savedVoice = repo.save(updatedVoice)
            
            ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(mapOf(
                    "code" to 200,
                    "message" to "更新成功",
                    "data" to savedVoice
                ))
        } catch (e: Exception) {
            ResponseEntity.badRequest()
                .contentType(MediaType.APPLICATION_JSON)
                .body(mapOf(
                    "code" to 400,
                    "message" to "更新失败: ${e.message}"
                ))
        }
    }
    
    @GetMapping("/{id}")
    fun getById(@PathVariable id: Long): ResponseEntity<Any> {
        return try {
            val voice = repo.findById(id).orElse(null)
            if (voice == null) {
                ResponseEntity.status(404)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(mapOf(
                        "code" to 404,
                        "message" to "声音不存在"
                    ))
            } else {
                ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(mapOf(
                        "code" to 200,
                        "message" to "success",
                        "data" to voice
                    ))
            }
        } catch (e: Exception) {
            ResponseEntity.badRequest()
                .contentType(MediaType.APPLICATION_JSON)
                .body(mapOf(
                    "code" to 400,
                    "message" to "获取失败: ${e.message}"
                ))
        }
    }
} 